<?php
namespace Admin\Model;

/**
 * 复制图片的模型
 * Class VendorModel
 * @package Admin\Model
 */
class VendorModel extends CommonModel {
	
	protected $connection = "MSDB_CONFIG";
	
	protected $db_name;

	//初始化 数据库链接
	public function __construct($name='',$tablePrefix='',$connection='') {
        // 模型初始化
        $this->_initialize();
        // 获取模型名称
        if(!empty($name)) {
            if(strpos($name,'.')) { // 支持 数据库名.模型名的 定义
                list($this->dbName,$this->name) = explode('.',$name);
            }else{
                $this->name   =  $name;
            }
        }elseif(empty($this->name)){
            $this->name =   $this->getModelName();
        }
        // 设置表前缀
        if(is_null($tablePrefix)) {// 前缀为Null表示没有前缀
            $this->tablePrefix = '';
        }elseif('' != $tablePrefix) {
            $this->tablePrefix = $tablePrefix;
        }else{
            $this->tablePrefix = $this->tablePrefix?$this->tablePrefix:C('DB_PREFIX');
        }

        // 数据库初始化操作
        // 获取数据库操作对象
        // 当前模型有独立的数据库连接信息
        $this->db('MSDB_CONFIG',empty($this->connection)?$connection:$this->connection);
    }

    /**
     * 获取字段
     * @param string $field
     * @param null $sepa
     * @return array|mixed|null
     */
	public function getField($field,$sepa=null) {
        $options['field']    =  $field;
        $options =  $this->_parseOptions($options);
        if(strpos($field,',')) { // 多字段
            $resultSet = $this->db->select($options);
            if(!empty($resultSet)) {
                $_field = explode(',', $field);
                $field  = array_keys($resultSet[0]);
                $move   =  $_field[0]==$_field[1]?false:true;
                $key =  array_shift($field);
                $key2 = array_shift($field);
                $cols   =   array();
                $count  =   count($_field);
                foreach ($resultSet as $result){
                    $name   =  $result[$key];
                    if($move) { // 删除键值记录
                        unset($result[$key]);
                    }
                    if(2==$count) {
                        $cols[$name]   =  $result[$key2];
                    }else{
                        $cols[$name]   =  is_null($sepa)?$result:implode($sepa,$result);
                    }
                }
                return $cols;
            }
        }else{   // 查找一条记录
            $options['limit'] = 1;
            $result = $this->db->select($options);
            if(!empty($result)) {
                return next($result[0]);
            }
        }
        
        return null;
    }

    /**
     * 翻译处理乱码
     * @param $resultSet
     * @param $options
     */
    protected function _after_select(&$resultSet,$options) {
    	foreach ($resultSet as $key=>$val){
    		 $resultSet[$key]['cVenName'] = iconv("gb2312","utf-8//IGNORE",$val['cVenName']);
    		 $resultSet[$key]['cVenAbbName'] = iconv("gb2312","utf-8//IGNORE",$val['cVenAbbName']);
    		 $resultSet[$key]['cVenPerson'] = iconv("gb2312","utf-8//IGNORE",$val['cVenPerson']);
    		 $resultSet[$key]['cVenAddress'] = iconv("gb2312","utf-8//IGNORE",$val['cVenAddress']);
    		 $resultSet[$key]['cVenBank'] = iconv("gb2312","utf-8//IGNORE",$val['cVenBank']);
    	}
    }
    
    
      protected function _after_db() {
    	$this->db->selectDb(C("MSDB_CONFIG.db_name"));
    }
}
?>
